package com.usermanage.controller;

import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.Service.UserService;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.util.JsonData;
import com.usermanage.util.StaticUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/custom")
public class CustomController {


    @Resource
    private UserService userService;

    //客户列表首页
    @RequestMapping(value = "/list.page", method = RequestMethod.GET)
    public ModelAndView list() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/custom/list");
        return mv;
    }

    /**
     * 用户首页列表数据
     *
     * @param key
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list.json", method = RequestMethod.POST)
    public JsonData roleListJson(String key, String page, String size) throws Exception {
        if (StringUtils.isBlank(page)) {
            page = StaticUtil.pageIndex;
        }
        if (StringUtils.isBlank(size)) {
            size = StaticUtil.sizeCount;
        }

        //得到用户的列表
        List<UserBean> list = userService.getCustomUserList(key, page, size);
//        //根据用户组的uuid得到用户的角色列表记录数
        int count = userService.getCustomUserListCount(key);
//        //从用户信息中获取权限列表，此处拼接列表的权限button按钮
        List<PermissionBean> permissionBeanList = SpringSecurityUtils.getCurrentUser().getList();
        List<Map> operaButton = new ArrayList<>();
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("/custom/cancel.json")) {
                Map map = new HashMap();
                map.put("name", bean.getPermission_name());
                map.put("fn", "del");
                operaButton.add(map);
            }
            if (bean.getPermission_url().equals("/custom/edit.page")) {
                Map map = new HashMap();
                map.put("name", bean.getPermission_name());
                map.put("fn", "modify");
                operaButton.add(map);
            }
        }
        if (StringUtils.isBlank(key)) {
            key = "''";
        }
        if (operaButton.size() == 0) {
            Map map = new HashMap();
            map.put("name", "--");
            operaButton.add(map);
        }
        Map resultMap = new HashMap();
        resultMap.put("list", list);
        resultMap.put("total", count == 0 ? 1 : count);
        resultMap.put("operaButton", operaButton);
        resultMap.put("key", key);
        resultMap.put("pageIndex", page);
        resultMap.put("size", size);
        return JsonData.success(resultMap);
    }

    //客户管理员账号添加
    @RequestMapping(value = "/add.page", method = RequestMethod.GET)
    public ModelAndView addPage() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/custom/add");
        return mv;
    }
}
